package com.ych.modules.common.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ych.modules.base.dbEntity.TUserFeedbackEntity;
import com.ych.modules.base.service.ITUserFeedbackService;
import com.ych.modules.common.entity.modify.ReplyFeedbackEntity;
import com.ych.modules.common.entity.query.QueryUserFeedbackEntity;
import com.ych.modules.common.service.UserFeedbackService;
import com.ych.utils.TLMap;
import com.ych.utils.UUID;
import com.ych.utils.beanToMap.BeanMapUtil;
import com.ych.utils.mysqlPageUtil.Query;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * Author: Usopp.tsui
 * Date: 2020/12/31
 * Time: 15:48
 * Description:
 */
@Service("UserFeedbackService")
public class UserFeedbackServiceImpl implements UserFeedbackService {
    @Autowired
    private ITUserFeedbackService itUserFeedbackService;

    @Override
    public Page<TUserFeedbackEntity> listUserFeedback(QueryUserFeedbackEntity entity) {
        Wrapper<TUserFeedbackEntity> wrapper = new EntityWrapper<TUserFeedbackEntity>()
                .where("1=1")
                .eq(StringUtils.isNotBlank(entity.getFeedbackType()), "feedback_type", entity.getFeedbackType())
                .eq(StringUtils.isNotBlank(entity.getStatus()), "status", entity.getStatus())
                .eq(StringUtils.isNotBlank(entity.getUserId()), "user_id", entity.getUserId())
                .orderBy("status desc,create_time asc");
        Page<TUserFeedbackEntity> page = itUserFeedbackService.selectPage(new Query<TUserFeedbackEntity>(BeanMapUtil.toPageQuery(entity)).getPage(), wrapper);
        return page;
    }

    @Override
    public void replyFeedback(ReplyFeedbackEntity entity) {
        TUserFeedbackEntity feedbackEntity = itUserFeedbackService.selectById(entity.getFeedbackId());
        if (null == feedbackEntity) {
            throw new RuntimeException("反馈不存在！有疑问请联系管理员！");
        }
        feedbackEntity.setReplyContent(entity.getReplyContent());
        feedbackEntity.setStatus("0");
        feedbackEntity.setReplyTime(new Date());
        feedbackEntity.setReplyUserId(TLMap.getUserId());
        itUserFeedbackService.updateById(feedbackEntity);
    }

    @Override
    public void addFeedback(TUserFeedbackEntity feedbackEntity) {
        feedbackEntity.setFeedbackId(UUID.randomUUID());
        feedbackEntity.setCreateTime(new Date());
        feedbackEntity.setStatus("1");//未回复
        itUserFeedbackService.insert(feedbackEntity);
    }

    @Override
    public TUserFeedbackEntity getFeedback(String feedbackId) {
        TUserFeedbackEntity entity = itUserFeedbackService.selectById(feedbackId);
        if (null == entity) {
            throw new RuntimeException("反馈不存在!");
        }
        return entity;
    }
}
